package com.sakura.service.impl;

import cn.hutool.core.util.IdcardUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sakura.dbo.ElderlyDBO;
import com.sakura.dbo.UnCheckElderlyDBO;
import com.sakura.dto.QueryElderlyDTO;
import com.sakura.dto.UnCheckElderlyDTO;
import com.sakura.entity.CheckProject;
import com.sakura.entity.Elderly;
import com.sakura.entity.Manager;
import com.sakura.service.CheckProjectService;
import com.sakura.service.CheckRecordService;
import com.sakura.service.ElderlyService;
import com.sakura.mapper.ElderlyMapper;
import com.sakura.service.ManagerService;
import com.sakura.util.EntityConverter;
import com.sakura.util.R;
import com.sakura.vo.ElderlyVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.beans.Beans;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Sakura
 * @description 针对表【elderly】的数据库操作Service实现
 * @createDate 2023-11-22 15:49:03
 */
@Service
public class ElderlyServiceImpl extends ServiceImpl<ElderlyMapper, Elderly>
        implements ElderlyService {
    @Autowired
    private ElderlyMapper elderlyMapper;

    @Autowired
    private CheckProjectService checkProjectService;

    @Autowired
    private ManagerService managerService;

    /**
     * 获取每天需要检查的老人列表
     *
     * @param :
     * @return void
     * @author Sakura
     * @date 2023/11/23 9:34
     */
    @Override
    public List<ElderlyDBO> getNeedCheckElderlyList() {
        //1、查出所有需要检查的老人
        List<ElderlyDBO> elderlyDBOList = elderlyMapper.getNeedCheckElderlyList();
        return elderlyDBOList;
    }

    /**
     * 只需要查出有哪些老人即可，至于老人具体有哪些没有做，可以分成两个接口
     * @param pageNo:
     * @param pageSize:
     * @param searchKey:
     * @return Map<String, Object>
     * @author Sakura
     * @date 2023/11/23 13:47
     */
    @Override
    public Map<String, Object> getUnCheckElderlyPage(Integer pageNo, Integer pageSize, String searchKey) {
        //查出未检查的老人
        List<UnCheckElderlyDBO> unCheckElderlyPage = elderlyMapper.getUnCheckElderlyPage((pageNo-1)*pageSize, pageSize, searchKey);
        //既然数据库记录表已经有老人了，只需要把检查记录未完成的老人查出来去重就行
        Map<String, Object> map = new HashMap<>();
        Long total = elderlyMapper.getNeedCheckElderlyTotal(searchKey);
        map.put("records", unCheckElderlyPage);
        map.put("total", total);
        return map;
    }

    @Override
    public Elderly getElderlyByPhone(String phone) {
        LambdaQueryWrapper<Elderly> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Elderly::getPhone, phone);
        return this.getOne(queryWrapper);
    }



    /**
     * 查出未检查的老人
     *
     * @param :
     * @return List<ElderlyDBO>
     * @author Sakura
     * @date 2023/11/23 10:49
     */
    @Override
    public List<UnCheckElderlyDTO> getUnCheckElderlyList() {
        //查出未检查的老人
        List<ElderlyDBO> unCheckElderlyList = elderlyMapper.getUnCheckElderlyList();
        //将DBO对象转换为DTO对象，同时将检查项目的名称传递给前端
        List<UnCheckElderlyDTO> unCheckElderlyDTOS = unCheckElderlyList.stream().map(item -> {
            UnCheckElderlyDTO unCheckElderlyDTO = EntityConverter.copyAndGetSingle(item, UnCheckElderlyDTO.class);
            CheckProject checkProject = checkProjectService.getById(item.getProjectId());
            unCheckElderlyDTO.setProjectName(checkProject.getName());
            return unCheckElderlyDTO;
        }).collect(Collectors.toList());
        return unCheckElderlyDTOS;
    }

    @Override
    public R getPage(QueryElderlyDTO queryElderlyVO) {
        LambdaQueryWrapper<Elderly> queryWrapper = new LambdaQueryWrapper<>();

        if (queryElderlyVO.getId() != null && queryElderlyVO.getId() != 0) {
            queryWrapper.eq(Elderly::getId, queryElderlyVO.getId());
        }
        if (queryElderlyVO.getName()!= null &&!"".equals(queryElderlyVO.getName())) {
            queryWrapper.like(Elderly::getName, queryElderlyVO.getName());
        }
        if (queryElderlyVO.getGridId() != null && queryElderlyVO.getGridId() != 0) {
            queryWrapper.eq(Elderly::getGridId, queryElderlyVO.getGridId());
        }
//        if (queryElderlyVO.getSex() != null) {
//            queryWrapper.eq(Elderly::getIdCard, queryElderlyVO.getSex());
//        }
        Page<Elderly> elderlyPage = this.page(new Page<>(queryElderlyVO.getCurrent(), queryElderlyVO.getPageSize()), queryWrapper);

        List<ElderlyVO> voList = elderlyPage.getRecords().stream().map(item -> {
            ElderlyVO elderlyVO = EntityConverter.copyAndGetSingle(item, ElderlyVO.class);

            String idCard = item.getIdCard();
            boolean validCard = IdcardUtil.isValidCard(idCard);
            if (validCard) {
                int age = IdcardUtil.getAgeByIdCard(idCard);
                int gender = IdcardUtil.getGenderByIdCard(idCard);
                elderlyVO.setSex(gender);
                elderlyVO.setAge(age);
            }
            Long gridId = item.getGridId();

            if (gridId != null) {
                Manager manager = managerService.getById(gridId);
                if (manager != null) {
                    //设置网格员信息
                    elderlyVO.setGridId(manager.getName());
                }
            }
            return elderlyVO;
        }).collect(Collectors.toList());

        Page<ElderlyVO> voPage = new Page<>();
        BeanUtils.copyProperties(elderlyPage, voPage, "records");
        voPage.setRecords(voList);
        return R.success(voPage);
    }
}




